package cn.classTest.mall.query;


import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.Collections;
import java.util.List;

@Getter
@Setter
@ToString
public class PageResult {

    //数据集
    private List data;

    //数据总条数
    private int totalCount;

    //总页数
    private int totalPage;

    //上一页
    private int prevPage;

    //下一页
    private int nextPage;

    //每页显示条数
    private int pageSize;

    //当前页
    private int currentPage;


    //构造器(缺参构造器)
    public PageResult(List data, int totalCount, int pageSize, int currentPage) {
        //1.直接给对应的成员变量赋值
        this.data = data;
        this.totalCount = totalCount;
        this.pageSize = pageSize;
        this.currentPage = currentPage;
        //2.给需要计算的成员变量赋值
        this.totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1;
        this.prevPage = currentPage - 1 > 1 ? currentPage - 1 : 1;
        this.nextPage = currentPage + 1 < totalPage ? currentPage + 1 : totalPage;
    }

    //这个构造器用来单独处理数据库中查询到的数据为0条的时候
    public PageResult(int pageSize, int currentPage){
        this.data = Collections.emptyList();
        this.totalCount = 0;
        this.pageSize = pageSize;
        this.currentPage = currentPage;
    }


}
